package com.example.xinli.controller;

import com.example.xinli.service.IToolService;

import com.example.xinli.domain.vo.ResponseBean;
import com.example.xinli.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

@Controller
@Slf4j
@RequestMapping("/upload")
@Api(tags = "测试 Swagger")
public class UploadController   {

    @Autowired
    private IToolService uploadService;

    @Autowired
    private IUserService userService;

    @PostMapping("/img")
    @ApiOperation("上传图片")
    public ResponseBean uploadImg(@RequestParam("img") MultipartFile imgFile,@RequestParam("name") String fileName) {
        return uploadService.uploadImg(imgFile,fileName);
    }

    @GetMapping("/excel/eval")
    @ResponseBody
    @ApiOperation("导出eval-excel")
    public ResponseBean evalExceldownload() {

        log.info("收到导出全部评价数据请求");
        // 1. 生成 Excel 文件
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        uploadService.downloadevalExcel(byteArrayOutputStream);


        // 2. 将 Excel 文件上传到 MinIO
        InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        String fileName = "eval_"+  System.currentTimeMillis()  + ".xlsx";

        long fileSize = byteArrayOutputStream.size();  // 获取生成的 Excel 文件的大小
        String contentType = "application/vnd.ms-excel";  // Excel 文件的 MIME 类型

// 4. 生成文件名


        // 假设上传到 MinIO 的方法是 uploadToMinio(inputStream, fileName)
        return uploadService.uploadFile(inputStream, fileName, fileSize, contentType);
    }


    @GetMapping("/singleexcel")
    @ResponseBody
    @ApiOperation("导出单个用户excel")
    public ResponseBean singleExceldownload(@RequestParam("id") String id) {
//        log.info("111");
//        try {
//            response.reset();
//            response.setContentType("application/vnd.ms-excel");
//            response.setHeader("Content-disposition",
//                    "attachment;filename=user_excel_" + System.currentTimeMillis() + ".xlsx");
//            uploadService.downloadExcel(response.getOutputStream());
//            log.info("下载成功");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
        log.info("收到导出单个用户请求");
        // 1. 生成 Excel 文件
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        uploadService.downloadSingleExcel(byteArrayOutputStream, id);

        //根据id去查找实验序号
        String exid=userService.getUserIdByOpenId(id);

        // 2. 将 Excel 文件上传到 MinIO
        InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        String fileName = exid + "_" + System.currentTimeMillis() +"_"+ "daka" + ".xlsx";

        long fileSize = byteArrayOutputStream.size();  // 获取生成的 Excel 文件的大小
        String contentType = "application/vnd.ms-excel";  // Excel 文件的 MIME 类型

// 4. 生成文件名


        // 假设上传到 MinIO 的方法是 uploadToMinio(inputStream, fileName)
        return uploadService.uploadFile(inputStream, fileName, fileSize, contentType);
    }

    @GetMapping("/excel/inter")
    @ResponseBody
    @ApiOperation("导出单个用户干预excel")
    public ResponseBean InterExceldownload(@RequestParam("id") String id) {
        log.info("收到导出单个用户请求");
        // 1. 生成 Excel 文件
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        uploadService.downloadInterResExcel(byteArrayOutputStream, id);

        String exid=userService.getUserIdByOpenId(id);
        // 2. 将 Excel 文件上传到 MinIO
        InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        String fileName = exid + "_" + System.currentTimeMillis() +"_"+ "ganyu" + ".xlsx";

        long fileSize = byteArrayOutputStream.size();  // 获取生成的 Excel 文件的大小
        String contentType = "application/vnd.ms-excel";  // Excel 文件的 MIME 类型

// 4. 生成文件名


        // 假设上传到 MinIO 的方法是 uploadToMinio(inputStream, fileName)
        return uploadService.uploadFile(inputStream, fileName, fileSize, contentType);
    }


    @GetMapping("/excel")
    @ResponseBody
    @ApiOperation("导出excel")
    public ResponseBean download() {
//        log.info("111");
//        try {
//            response.reset();
//            response.setContentType("application/vnd.ms-excel");
//            response.setHeader("Content-disposition",
//                    "attachment;filename=user_excel_" + System.currentTimeMillis() + ".xlsx");
//            uploadService.downloadExcel(response.getOutputStream());
//            log.info("下载成功");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
        log.info("收到请求");
        // 1. 生成 Excel 文件
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        uploadService.downloadExcel(byteArrayOutputStream);

        // 2. 将 Excel 文件上传到 MinIO
        InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        String fileName = "user_excel_" + System.currentTimeMillis() + ".xlsx";

        long fileSize = byteArrayOutputStream.size();  // 获取生成的 Excel 文件的大小
        String contentType = "application/vnd.ms-excel";  // Excel 文件的 MIME 类型

// 4. 生成文件名


        // 假设上传到 MinIO 的方法是 uploadToMinio(inputStream, fileName)
        return uploadService.uploadFile(inputStream, fileName, fileSize, contentType);
    }


}

